from selenium import webdriver
from selenium.webdriver.common.by import By

from common.DriverTools import DriverTools
from common.utils import get_el_text
from page.HomePage import HomePage
from page.RegisterPage import RegisterPage


class TestRegister(object):
    def setup_class(self):
        # # 1、初始化驱动对象
        self.driver=DriverTools.get_driver()
        self.reg_login=RegisterPage()

    def teardown_class(self):
        # 4、关闭浏览器驱动对象
        # self.driver.quit()
        DriverTools.quit_driver()

    def setup(self):
        # 2、打开注册页面
        self.driver.get("http://hmshop-test.itheima.net/")
        HomePage().goto_register_page()

    def teardown(self):
        try:
            self.driver.find_element(By.LINK_TEXT,'安全退出').click()
        except Exception:
            print("当前处于未登录状态")

    def test_register_success(self):
        # 3、填写注册信息，点击注册，并断言
        self.reg_login.register("13012125599","8888","123456","123456")
        name=get_el_text(self.driver,By.CLASS_NAME,'userinfo')
        assert "13012125599"==name

    def test_register_fail_phone_exist(self):
        # 3、填写注册信息，点击注册，并断言
        self.reg_login.register("13012125599", "8888", "123456", "123456")
        alert_text=get_el_text(self.driver,By.CLASS_NAME,'layui-layer-content')
        assert "该手机号已注册"==alert_text

    def test_register_fail_code_error(self):
        # 3、填写注册信息，点击注册，并断言
        self.reg_login.register("13012125500", "1234", "123456", "123456")
        alert_text=get_el_text(self.driver,By.CLASS_NAME,'layui-layer-content')
        assert "验证码错误" in alert_text